package com.crack.nga2.utils;


import android.util.Base64;
import android.util.Log;

import com.crack.nga2.entity.Theme;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2015/12/22.
 */
public class DataUtil {
    /**
     * 截取有效数据
     *
     * @param string 原始数据
     * @return data部分
     */
    public static String getData(String string) {
        string = string.replaceAll("\"sub_forums\":\"\",", "");
        string = string.substring(string.indexOf("{"));
        Gson gson = new Gson();
        Type type = new TypeToken<Map<String, Object>>() {
        }.getType();
        Map<String, Object> map = gson.fromJson(string, type);
        return gson.toJson(map.get("data"));
    }

    /**
     * 处理标题
     */
    public static void pauseSubject(Theme theme) {
        String subject = theme.getSubject();
        //存储标题主题分类部分
        StringBuilder sb = new StringBuilder();
        Pattern p = Pattern.compile("(?<=\\[)(.*)(?=\\])");
        Matcher m = p.matcher(subject);
        int end = 0;
        while (m.find()) {
            end = m.end() + "]".length();
            sb.append("[").append(m.group()).append("] ");
        }
        //正文部分
        String text = subject.substring(end);
        StringBuilder result = new StringBuilder(text);
        String misc = theme.getTopic_misc();
        try {
            base64(result, misc);
        } catch (Exception e) {
            e.printStackTrace();
        }
        theme.setSubject(sb.toString() + result.toString());
    }

    private static void base64(StringBuilder result, String misc) {
        if (misc != null && misc.length() > 0) {
            if (!misc.startsWith("~")) {
                byte[] bytes = Base64.decode(misc, Base64.DEFAULT);
                Log.i("ForumAc", Arrays.toString(bytes));
                if (bytes[0] == 1) {//设置颜色
                    int style = byte4ToInt(bytes, 1);
                    String str = Integer.toBinaryString(style);
                    boolean[] flags = new boolean[8];
                    for (int i = 0; i < str.length() && i < flags.length; i++) {
                        flags[i] = str.charAt(str.length() - i - 1) == '1';
                    }
                    if (flags[0]) {
                        result.insert(0, "<font color=red>").append("</font>");
                    }
                    if (flags[1]) {
                        result.insert(0, "<font color=blue>").append("</font>");
                    }
                    if (flags[2]) {
                        result.insert(0, "<font color=green>").append("</font>");
                    }
                    if (flags[3]) {
                        result.insert(0, "<font color=orange>").append("</font>");
                    }
                    if (flags[4]) {
                        result.insert(0, "<font color=silver>").append("</font>");
                    }
                    if (flags[5]) {
                        result.insert(0, "<b>").append("</b>");
                    }
                    if (flags[6]) {
                        result.insert(0, "<i>").append("</i>");
                    }
                    if (flags[7]) {
                        result.insert(0, "<u>").append("</u>");
                    }
                }
            }
        }
    }


    /**
     * byte数组转换为int整数
     *
     * @param bytes byte数组
     * @param off   开始位置
     * @return int整数
     */
    public static int byte4ToInt(byte[] bytes, int off) {
        int b0 = bytes[off] & 0xFF;
        int b1 = bytes[off + 1] & 0xFF;
        int b2 = bytes[off + 2] & 0xFF;
        int b3 = bytes[off + 3] & 0xFF;
        return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
    }

}
